import copy

import pygame


class BlockAction:
    # 绘制所有方块,自动排序
    def drawAll(block_list,re,screen):
        # block_list.text进行排序处理
        text_List = []
        for block in block_list:
            if block is not None:
                text_List.extend([ord(char) for char in block.text])
        text_List.sort();
        if re == -1:
            block_list.reverse()
            text_List.reverse()
        if len(text_List)>0:
            for index,block in enumerate(block_list):
                block.setText(pygame,str(chr(text_List[index])))
        for block in block_list:
            if block is not None:
                block.draw(screen,pygame)

    # 绘制所有方块，无排序
    def drawAllNoOrder(block_list,screen):
        for block in block_list:
            block.draw(screen,pygame)
    # 移动list所有方块
    def moveAll(stepx,stepy,block_list,keyEvent,block_last):
        # global block_last
        # if block_last == None or len(block_last)==0:
        #     block_last = []
        #     block_last.append(1)
        block_last[0] = copy.copy(block_list[len(block_list)-1])
        block_start = block_list[0]
        block_end = copy.copy(block_list.pop())
        new_x = 0
        new_y = 0
        if keyEvent == "left":
            new_x = -1
        if keyEvent == "right":
            new_x = 1
        if keyEvent == "up":
            new_y = -1
        if keyEvent == "down":
            new_y = 1
        block_end.x = block_start.x + new_x * block_start.width
        block_end.y = block_start.y + new_y * block_start.height
        block_list.insert(0,block_end)
        # block_end.move(stepx,stepy)
        # for block in block_list:
        #     block.move(stepx,stepy)
